import Vue from 'vue'
import VueRouter from 'vue-router'
// import Home from '../views/Home.vue'
import login from '../components/login.vue'
import home from '../components/home.vue'
import welcome from '../components/welcome.vue'
import users from '../components/users/users.vue'
import roles from '../components/rights/role.vue'
import rights from '../components/rights/right.vue'
import goods from '../components/goods_cate/goods.vue'
import params from '../components/goods_cate/goods_params.vue'
import categories from '../components/goods_cate/categories.vue'
import add from '../components/goods_cate/add.vue'

Vue.use(VueRouter)

  const routes = [
    {path:'/',redirect:'/login',component:login},
  
    {path:'/login',component:login},
    {path:'/home',component:home,redirect:'/welcome',
      children:[
        {
          path:'/welcome',component:welcome},
          {path:'/users',component:users},
          {path:'/roles',component:roles},
          {path:'/rights',component:rights},
          {path:'/goods',component:goods},
          {path:'/params',component:params},
          {path:'/categories',component:categories},
          {path:'/goods/add',component:add}
        ]
  }

]

const router = new VueRouter({
  routes
})
router.beforeEach((to,from,next)=>{

  if(to.path==='/login') return next()
  const tokenStr = sessionStorage.getItem('token')
  if(!tokenStr) return next('/login')
  return next()

})

export default router
